$(function() {
	tagOnChange();
	createEduSelect(document.getElementById("eid"));
	createNationSelect(document.getElementById("nid"));

	$.ajax({
		url : rootPath + '/tag/selectPageTag',
		type : 'POST',
		async : false,
		data : JSON.stringify({
			"dr" : 1
		}),
		contentType : 'application/json;charset=UTF-8',
		success : function(data) {
			if (data.returnCode == '0000') {
				fillTag(data);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});

	$("#switchTag").click(function() {
		var pageNo = parseInt($("#pageNo").val());
		var page = parseInt($("#page").val());
		if(page == 1) {
			pageNo = 2;
		} else {			
			pageNo = (pageNo == page) ? 1 : pageNo + 1;
		}
		$("#pageNo").val(pageNo);
		$.ajax({
			url : rootPath + '/tag/selectPageTag',
			type : 'POST',
			data : JSON.stringify({
				"pageNo" : pageNo,
				"dr" : 1,
			}),
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				if (data.returnCode == '0000') {
					fillTag(data);
					var length = $("#selectTags").val();
					var tagName = new Array();
					tagName = length.split(",");
					$(".tag").each(function() {
						for (var i = 0; i < tagName.length; i++) {
							// 比较标签是否已选中
							if (tagName[i] == $(this).html()) {
								$(this).addClass("tagSeleted");
								$(this).prop("disabled", "disabled");
							}
						}
					}); 
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error");
			}
		});

	});

	// 反显数据
	compete();

});

function fillTag(data) {
	if (data.returnCode == '0000') {
		$("#page").val(data.returnObject.pages);
		var length = data.returnObject.list.length;
		var html = '';
		if(length == 0){
			layer.alert("没有更多的数据了！");
			return;
		}
		$(".tagContent .taglist").empty();

		for (var i = 0; i < length; i++) {
			if (i == 3) {
				html += "</br>"
			}
			html += "<li class='tag' id=tag_" + data.returnObject.list[i].tagId
					+ ">" + data.returnObject.list[i].tagName + "</li>";
		}
		$(".tagContent .taglist").append(html);
		$(".tag").off("click")
		  .on("click",_aj22euu9);
	}
}

function _aj22euu9() {
	if ($(this).attr("class").match("tagSeleted")) {
		var arr = $("#selectTags").val();
		var tagName = new Array();
		tagName = arr.split(",");
		$(this).removeClass("tagSeleted");
		let index = tagName.indexOf($(this).html());
		tagName.splice(index, 1);
		$("#selectTags").val(tagName);
	} else {
		$(this).addClass("tagSeleted");
		if ($("#selectTags").val() == "") {
			$("#selectTags").val($(this).html());
		} else {
			$("#selectTags").val($('#selectTags').val() + "," + $(this).html());
		}
	}
}

function compete() {
	$.ajax({
		url : rootPath + "/userDetailFront/queryDetailInfo",
		method : "GET",
		data : {
			"tagFlag" : "true"
		},
		success : function(data) {
			if (data.returnCode == '0000') {
				if (data.returnObject != null) {
					var obj = data.returnObject;
					if (obj.nid != null && obj.nid != "") {
						$("#nid option[value='" + obj.nid + "']").prop("selected", "selected");
					}
					if (obj.eid != null && obj.eid != "") {
						$("#eid option[value='" + obj.eid + "']").prop("selected", "selected");
					}
					if (obj.company != null && obj.company != "") {
						$("#company").val(obj.company);
					}
					if (obj.profession != null && obj.profession != "") {
						$("#profession").val(obj.profession);
					}
					if (obj.tags != null && obj.tags != "") {
						var length = obj.tags.length;
						var result = "";
						for (var i = 0; i < length; i++) {
							// 比较标签是否已选中
							isExist(obj.tags[i].tagName);
							result = result + obj.tags[i].tagName + ",";
						}
						result = result.substring(0, result.length - 1);
						$("#selectTags").text(result);
					}
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});

}
// 比较标签是否已选中
function isExist(tagName) {
	$(".tag").each(function() {
		if ($(this).html() == tagName) {
			$(this).addClass("tagSeleted");
			$(this).prop("disabled", "disabled");
		}
	});
}

// 根据标签框的变化做出判断
function tagOnChange() {
	$("#selectTags").bind('input propertychange', function() {
		var text = $("#selectTags").val();
		var tagName = new Array();
		tagName = text.split(",");
		$(".tag").each(function() {
			$(this).removeClass("tagSeleted");
			$(this).prop("disabled", false);
		});
		for (var i = 0; i < tagName.length; i++) {
			isExist(tagName[i]);
		}
	});
}